package com.mysoap.medicine.service;

import java.text.ParseException;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.bs.entity.MedicineCategory;
import com.bs.entity.MedicineIn;
import com.bs.entity.MedicineInfo;
import com.bs.entity.MedicineOut;
import com.bs.entity.MedicineStorage;
import com.bs.service.impl.MedicineInfoServiceImpl;
import com.mysoap.medicine.bean.MedicineInfoRequest;
import com.mysoap.medicine.bean.MedicineInfoResponse;
import com.mysoap.medicine.bean.SetUtils;
import com.mysoap.medicine.client.DateHelper;
import com.mysoap.medicine.client.MedicinePropTool;
/**
 * 药品信息服务
 * @author shimh
 *
 */
public class MedicineInfoSoap {
	private MedicineInfoServiceImpl medicineInfoService;

	public MedicineInfoServiceImpl getMedicineInfoService() {
		return medicineInfoService;
	}

	public void setMedicineInfoService(MedicineInfoServiceImpl medicineInfoService) {
		this.medicineInfoService = medicineInfoService;
	}
	
	/**
	 * 保存药品信息
	 * @throws Exception 
	 */
	public MedicineInfoResponse medicineSave(MedicineInfoRequest medicineRequest) throws Exception{
		System.out.println("-------------------save");
		MedicineInfoResponse response = new MedicineInfoResponse();
		MedicineInfo info = MedicinePropTool.deal(medicineRequest);
		System.out.println("id"+info.getMedicineId());
		boolean result = medicineInfoService.addMedicineInfo(info);
		System.out.println("result"+result);
		if(result){
			response.setDiscription("操作成功");
			response.setResultCode(0);
		}else{
			response.setDiscription("保存失败");
			response.setResultCode(2);
		}
		return response;
	}
	
	/**
	 * 获取药品查询信息列表
	 * @throws Exception 
	 */
	public MedicineInfoResponse medicineQuery(MedicineInfoRequest request) throws Exception{
		MedicineInfoResponse response = new MedicineInfoResponse();
		int updateFlag = request.getUpdateFlag();
		System.out.println("==========================="+updateFlag);
		if(updateFlag==1){//获取药品信息列表
			List<MedicineInfo> medList = medicineInfoService.getAllMedicineList();
			String str ="";
			
			if(medList.size()>0){
				MedicineInfo[] medArr = new MedicineInfo[medList.size()];
				MedicineIn[] medicineInArr = new MedicineIn[medList.size()];
				MedicineOut[] medicineOutArr = new MedicineOut[medList.size()];
				for(int i=0;i<medList.size();i++){
					MedicineInfo mi = new MedicineInfo();	
					String date = DateHelper.getTimeStamp(medList.get(i).getBuyTime(), new String[]{"yyyy-MM-dd HH:mm:ss"});
		//			mi.setBuyTime(medList.get(i).getBuyTime());
					mi.setMedicineCategory(medList.get(i).getMedicineCategory());
					mi.setMedicineCode(medList.get(i).getMedicineCode());
					mi.setMedicineDesc(medList.get(i).getMedicineDesc());
					mi.setMedicineFactory(medList.get(i).getMedicineFactory());
					mi.setMedicineFactoryAddress(medList.get(i).getMedicineFactoryAddress());
					mi.setMedicineId(medList.get(i).getMedicineId());
					mi.setMedicineName(medList.get(i).getMedicineName());
					
					mi.setMedicinePrice(medList.get(i).getMedicinePrice());
					mi.setMedicineStorage(medList.get(i).getMedicineStorage());
					medArr[i] = mi;
					for(MedicineOut out: medList.get(i).getMedicineOuts()){
						medicineOutArr[i] = out;
					}
					for(MedicineIn in: medList.get(i).getMedicineIns()){
						medicineInArr[i] = in;
					}
					System.out.println(medicineOutArr.length);
					str+=date+",";
				}
				System.out.println("medArr:"+medArr.length);
				response.setDiscription("操作成功");
				response.setResultCode(0);
				response.setMedlist(medArr);
				response.setMedicineOuts(medicineOutArr);
				response.setMedicineIns(medicineInArr);
				response.setDateArr(str);
				
			}else{
				response.setDiscription("没有结果");
				response.setResultCode(3);
			}
		}else if(updateFlag==2){//药品信息模糊查找
			List<MedicineInfo> medList = medicineInfoService.getMedicineListByKeyWord(request.getKeyWord());
			if(medList.size()>0){
				MedicineInfo[] medArr = new MedicineInfo[medList.size()];
				MedicineIn[] medicineInArr = new MedicineIn[medList.size()];
				MedicineOut[] medicineOutArr = new MedicineOut[medList.size()];
				MedicineInfo mi = new MedicineInfo();
				for(int i=0;i<medList.size();i++){
				//	mi.setBuyTime(medList.get(i).getBuyTime());
					mi.setMedicineCategory(medList.get(i).getMedicineCategory());
					mi.setMedicineCode(medList.get(i).getMedicineCode());
					mi.setMedicineDesc(medList.get(i).getMedicineDesc());
					mi.setMedicineFactory(medList.get(i).getMedicineFactory());
					mi.setMedicineFactoryAddress(medList.get(i).getMedicineFactoryAddress());
					mi.setMedicineId(medList.get(i).getMedicineId());
				//	mi.setMedicineIns(medList.get(i).getMedicineIns());
					mi.setMedicineName(medList.get(i).getMedicineName());
				//	mi.setMedicineOuts(medList.get(i).getMedicineOuts());
					mi.setMedicinePrice(medList.get(i).getMedicinePrice());
					mi.setMedicineStorage(medList.get(i).getMedicineStorage());
					medArr[i] = mi;
					for(MedicineOut out: medList.get(i).getMedicineOuts()){
						medicineOutArr[i] = out;
					}
					for(MedicineIn in: medList.get(i).getMedicineIns()){
						medicineInArr[i] = in;
					}
				}
				System.out.println("medArr:"+medArr);
				response.setDiscription("操作成功");
				response.setResultCode(0);
				response.setMedlist(medArr);
				response.setMedicineOuts(medicineOutArr);
				response.setMedicineIns(medicineInArr);
			}else{
				response.setDiscription("没有结果");
				response.setResultCode(3);
			}
		}else if(updateFlag==3){//搜出快速匹配的关键字
			List<MedicineInfo> medList = medicineInfoService.getKeyWords(request.getKeyWord());
			if(medList.size()>0){
				MedicineInfo[] medArr = new MedicineInfo[medList.size()];
				MedicineIn[] medicineInArr = new MedicineIn[medList.size()];
				MedicineOut[] medicineOutArr = new MedicineOut[medList.size()];
				MedicineInfo mi = new MedicineInfo();
				for(int i=0;i<medList.size();i++){
				//	mi.setBuyTime(medList.get(i).getBuyTime());
					mi.setMedicineCategory(medList.get(i).getMedicineCategory());
					mi.setMedicineCode(medList.get(i).getMedicineCode());
					mi.setMedicineDesc(medList.get(i).getMedicineDesc());
					mi.setMedicineFactory(medList.get(i).getMedicineFactory());
					mi.setMedicineFactoryAddress(medList.get(i).getMedicineFactoryAddress());
					mi.setMedicineId(medList.get(i).getMedicineId());
					//mi.setMedicineIns(medList.get(i).getMedicineIns());
					mi.setMedicineName(medList.get(i).getMedicineName());
				//	mi.setMedicineOuts(medList.get(i).getMedicineOuts());
					mi.setMedicinePrice(medList.get(i).getMedicinePrice());
					mi.setMedicineStorage(medList.get(i).getMedicineStorage());
					medArr[i] = mi;
					for(MedicineOut out: medList.get(i).getMedicineOuts()){
						medicineOutArr[i] = out;
					}
					for(MedicineIn in: medList.get(i).getMedicineIns()){
						medicineInArr[i] = in;
					}
				}
				response.setDiscription("操作成功");
				response.setResultCode(0);
				response.setMedlist(medArr);
				response.setMedicineOuts(medicineOutArr);
				response.setMedicineIns(medicineInArr);
			}else{
				response.setDiscription("没有结果");
				response.setResultCode(3);
			}
		}else if(updateFlag==4){//根据Id获取对象
			MedicineInfo med = medicineInfoService.getMedicineInfoById(request.getMedicineId().toString());
			
			if(med==null){
				response.setDiscription("用户不存在");
				response.setResultCode(1);
			}else{
				response.setDiscription("操作成功");
				response.setResultCode(0);
			}
		}else if(updateFlag==5){//编辑常量对象
			MedicineInfo med = medicineInfoService.getMedicineInfoById(request.getMedicineId().toString());
			Date date = DateHelper.string2Date(request.getBuyTime());
			med.setBuyTime(date);
			med.setMedicineCode(request.getMeidcineCode());
			med.setMedicineDesc(request.getMedicineDesc());
			med.setMedicineFactory(request.getMedicineFactory());
			med.setMedicineFactoryAddress(request.getMedicineFactoryAddress());
			med.setMedicineName(request.getMedicineName());
			med.setMedicinePrice(request.getMedicinePrice());
			
			MedicineCategory mc = request.getMedicineCategory();
			System.out.println("-------------mc"+mc);
			MedicineStorage ms = request.getMedicineStorage();
			MedicineIn[] ins = request.getMedicineIns();
			Set<MedicineIn> sIns = new HashSet<MedicineIn>();
			MedicineOut[] outs = request.getMedicineOuts();
			Set<MedicineOut> sOuts = new HashSet<MedicineOut>();
			if(mc !=null){
				med.setMedicineCategory(mc);
			}
			if(ms !=null){
				med.setMedicineStorage(ms);
			}
			
			if(ins !=null){
				sIns = SetUtils.arrToSet(ins);
				med.setMedicineIns(sIns);
			}
			
			if(outs !=null){
				sOuts = SetUtils.arrToSet(outs);
				med.setMedicineOuts(sOuts);
			}
			boolean result = medicineInfoService.editMedicine(med);
			if(result){
				response.setDiscription("操作成功");
				response.setResultCode(1);
			}else{
				response.setDiscription("编辑失败");
				response.setResultCode(4);
			}
			
		}
		
		return response;
	}

}
